/**
 * 
 */
package com.wolfpire.workflow.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wolfpire.system.common.EasyuiPage;
import com.wolfpire.system.common.EasyuiPagination;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.Result;
import com.wolfpire.workflow.om.WfType;
import com.wolfpire.workflow.spring.WfTypeService;

/**
 * 流程类型控制器
 * @author lihd
 *
 */

@Controller
@RequestMapping("/wfType")
public class WfTypeController {
	
	@Resource private WfTypeService wfTypeService;
	
	@RequestMapping("/list")
	public ModelAndView list() {
		return new ModelAndView("workflow/wfType/wfType_list");
	}
	
	@ResponseBody
	@RequestMapping("/queryList")
	public EasyuiPagination<WfType> queryList(EasyuiPage<WfType> page, WfType wfType) {
		if (null != wfType && null != wfType.getId()) {
			wfType.setParentId(wfType.getId());
			wfType.setId(null);
		}
		Page<WfType> wfTypes = this.wfTypeService.setPageDataList(page, wfType);
		return new EasyuiPagination<WfType>(wfTypes);
	}
	
	@ResponseBody
	@RequestMapping("/wfTypes")
	public List<WfType> wfTypes(WfType wfType) {
		if (null != wfType && null != wfType.getId()) {
			wfType.setParentId(wfType.getId());
			wfType.setId(null);
		}
		List<WfType> lists = this.wfTypeService.list(wfType);
		/*List<Map<String, Object>> rs = new ArrayList<Map<String, Object>>();
		for (WfType	wf : lists) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", wf.getId());
			map.put("text", wf.getTypeName());
			map.put("parentId", wf.getParentId());
			map.put("_parentId", wf.getParentId());
			rs.add(map);
		}
		return rs;*/
		return lists;
	}
	
	@RequestMapping(value = "/add")
	public ModelAndView add(HttpSession session, Long parentId) {
		ModelAndView mv = new ModelAndView("workflow/wfType/wfType_add");
		mv.getModelMap().put("parentId", parentId);
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/save")
	public Result save(WfType wfType) {
		try {
			this.wfTypeService.saveOrUpdate(wfType);
			return new Result(true, "保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "保存失败，请联系管路员");
		}
	}
	
	@RequestMapping("/edit")
	public ModelAndView edit(Long id) {
		ModelAndView mv = new ModelAndView("workflow/wfType/wfType_edit");
		if (null != id) {
			WfType wfType = this.wfTypeService.get(id);
			mv.getModelMap().put("wfType", wfType);
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public Result delete(@RequestParam(value="id")Long id) {
		try {
			this.wfTypeService.delete(id);
			return new Result(true, "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "删除失败");
		}
	}
	
}
